package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchaseBiddingPlanTwotzDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchaseBiddingPlanTwotz;
import com.guodu.entity.PurchasebiddingtwotzStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchaseBiddingPlanTwotzService;
import com.guodu.vo.PurchaseBiddingPlanTwotzQueryVo;
import com.guodu.vo.PurchaseBiddingPlanTwotzVo;

@Service("purchaseBiddingPlanTwotzService")
public class PurchaseBiddingPlanTwotzServiceImpl implements PurchaseBiddingPlanTwotzService {
	
	
	@Resource
	private PurchaseBiddingPlanTwotzDao purchaseBiddingPlanTwotzDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;

	@Override
	public long findSizeByVo(PurchaseBiddingPlanTwotzQueryVo pbpq,Units units) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwotzDao.findSizeByVo(pbpq, units);
	}

	@Override
	public List<PurchaseBiddingPlanTwotzVo> findByVo(
			PurchaseBiddingPlanTwotzQueryVo pbpq, Units units, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<PurchaseBiddingPlanTwotz> list = purchaseBiddingPlanTwotzDao.findByVo(pbpq,units, curPage, pageSize);
		List<PurchaseBiddingPlanTwotzVo> purchaseBiddingPlanTwotzVos = new ArrayList<PurchaseBiddingPlanTwotzVo>();
		for(PurchaseBiddingPlanTwotz purchaseBiddingPlanTwotz : list ){
			PurchaseBiddingPlanTwotzVo purchaseBiddingPlanTwotzVo = new PurchaseBiddingPlanTwotzVo(purchaseBiddingPlanTwotz);
			UserInfo userInfo = userInfoDao.findById(purchaseBiddingPlanTwotz.getCreatePeople());
			Units unitsInfo = unitsDao.findById(purchaseBiddingPlanTwotz.getUnitsId());
			purchaseBiddingPlanTwotzVo.setUnitsName(unitsInfo.getUnitsName());
			purchaseBiddingPlanTwotzVo.setIndustry(unitsInfo.getIndustry());
			Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
			purchaseBiddingPlanTwotzVo.setUnitsLevel(createUnits.getUnitsLevel());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				purchaseBiddingPlanTwotzVo.setFunitsName(funitsInfo.getUnitsName());
			}
			purchaseBiddingPlanTwotzVo.setUserName(userInfo.getUserName());
			purchaseBiddingPlanTwotzVos.add(purchaseBiddingPlanTwotzVo);
		}
		return purchaseBiddingPlanTwotzVos;
	}

	@Override
	public void save(PurchaseBiddingPlanTwotz purchaseBiddingPlanTwotz) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwotzDao.save(purchaseBiddingPlanTwotz);
	}

	@Override
	public PurchaseBiddingPlanTwotz findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwotzDao.findById(purchaseId);
	}

	@Override
	public void update(PurchaseBiddingPlanTwotz purchaseBiddingPlanTwotz,Long status) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwotzDao.update(purchaseBiddingPlanTwotz);
		List<PurchasebiddingtwotzStatus> entities = new ArrayList<PurchasebiddingtwotzStatus>();
		PurchasebiddingtwotzStatus entity = new PurchasebiddingtwotzStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchaseBiddingPlanTwotz.getCreatePeople());
		entity.setPurchaseId(purchaseBiddingPlanTwotz.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchaseBiddingPlanTwotzDao.changeStatus(entities);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwotzDao.delByIds(delIds);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasebiddingtwotzStatus> entities = new ArrayList<PurchasebiddingtwotzStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasebiddingtwotzStatus entity = new PurchasebiddingtwotzStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchaseBiddingPlanTwotzDao.changeStatus(entities);
		purchaseBiddingPlanTwotzDao.examine(ids,status);
	}

	@Override
	public List<PurchasebiddingtwotzStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwotzDao.findStatus(purchaseId);
	}

	@Override
	public List<PurchaseBiddingPlanTwotz> findAllByVo(
			PurchaseBiddingPlanTwotzQueryVo pbpq, Units attribute) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwotzDao.findAllByVo(pbpq,attribute);
	}

	@Override
	public PurchaseBiddingPlanTwotzVo findAllMoney(
			PurchaseBiddingPlanTwotzQueryVo pbpq, Units units) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanTwotzDao.findAllMoney(pbpq, units);
	}

	@Override
	public void examinAll(PurchaseBiddingPlanTwotzQueryVo pbpq, Units units) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanTwotzDao.examinAll(pbpq,units);
	}

}
